/*
 *  Copyright:  (C) 2018 LingYun IoT System Studio
 *              All rights reserved.
 *
 *  File name:  fork.c
 *Description:  Usage of fork().
 *
 *    Version:  1.1
 *     Author:  Guo Wenxue <guowenxue@gmail.com>
 *		        Copied by Zhu Tianyin <zhuty_cn@163.com>
 *  ChangeLog:  1.0  Release initial version in 2018.
 *              1.1  Copy and change some details on "01/09/2021".
 */
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

int main(int argc, char **argv)
{
	pid_t pid;

	printf("Parent process PID[%d] start running...\n", getpid());
	pid = fork();
	if(pid<0)
	{
		printf("fork() create child process failure: %s\n", strerror(errno));
		return -1;
	}
	else if(pid == 0)       //(1)
	{
		printf("Child process PID[%d] start running, my parent PID is [%d]\n", getpid(), getppid());
		return 0;           //Use "return" to let progress exit.
	}
	else                    //(2)   (1)or(2),who executes first,depending on the Linux system.
							//Programmers can use processes to communicate with each other to solve this problem.
	{
		printf("Parent process PID[%d] continue running, and child process PID is [%d]\n", getpid(), pid);
		return 0;
	}
}
